package com.fowo.api.service.handleWork;

import com.fowo.api.entity.PurchaseRgoodsOrder;
import com.fowo.api.model.payable.PayableVo;
import com.fowo.api.model.payable.product.PayableProductItemVo;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderApprovalAction;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderVo;
import com.fowo.api.model.purchase.rgoods.order.product.PurchaseRgoodsOrderProductItemVo;
import com.fowo.api.service.PayableService;
import com.fowo.api.service.PurchaseRgoodsOrderService;
import com.fowo.api.service.impl.PurchaseRgoodsOrderServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class PurchaseRgoodsOrderApproval implements PurchaseRgoodsOrderApprovalAction {

    @Resource
    private PurchaseRgoodsOrderServiceImpl service;

    @Resource
    private PayableService payableService;

    @Override
    public void run(Long id) throws Exception {
        PurchaseRgoodsOrderVo vo = service.getVoById(id);
        Assert.notNull(vo,"没找到这个订单");
        Assert.isTrue(vo.getStatus().equals("0"),"该订单已审核!");
        addPayable(vo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addPayable(PurchaseRgoodsOrderVo vo){
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();

        PayableVo model = new PayableVo();
        model.setSupplierId(vo.getSupplierId());
        model.setPurchasesOrg(vo.getDept());
        model.setPayOrg(vo.getDept());
        model.setSettlementOrg(vo.getDept());
        model.setBusinessDate(new Date());
        model.setStauts("0");
        model.setPushStatus("0");
        model.setPreparedBy(jwtUserInfo.getUserId());
//        model.setProductType("1");
        model.setRemarks("退货单："+vo.getOrderNo());

        List<PayableProductItemVo> productltem = new ArrayList<>();
        PayableProductItemVo payableProductItemVo;
        for(PurchaseRgoodsOrderProductItemVo product : vo.getProducts()){
            payableProductItemVo = new PayableProductItemVo();
            payableProductItemVo.setProductId(product.getProductId());
            payableProductItemVo.setProductIdName(product.getProductName());
            payableProductItemVo.setSku(product.getProductSku());
            payableProductItemVo.setNum(-product.getQuantity());
            payableProductItemVo.setSubtotal(product.getAmount().negate());
            productltem.add(payableProductItemVo);
        }
        model.setProductltem(productltem);

        PurchaseRgoodsOrder order = new PurchaseRgoodsOrder();
        order.setId(vo.getId());
        order.setStatus("1");
        try {
            payableService.create(model);
            service.updateById(order);
        } catch (Exception e) {
            throw new RuntimeException("退货单生成应付单异常");
        }
    }
}
